package py.edu.ucsa.sisale.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import py.edu.ucsa.sisale.access.dao.UserRepository;
import py.edu.ucsa.sisale.component.UserSessionComponent;
import py.edu.ucsa.sisale.form.UserForm;

@Controller
@RequestMapping
public class AccessController {

    @Autowired
    private UserSessionComponent userSessionComponent;
    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/app/login")
    public String login(Model model, @RequestParam(required=false) String message){
        model.addAttribute("message", message);
        System.out.println("entra");
        return "/app/access/login";
    }

    @RequestMapping("/app/login/success")
    public String loginSuccess(Model model){
        userSessionComponent.setCurrentUser(userRepository.findUserByUsername(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()));
        model.addAttribute("userSessionComponent", userSessionComponent);
        if (userSessionComponent.getCurrentUser().isAccountLocked()){
        	UserForm form = new UserForm();
        	form.setUsername(userSessionComponent.getCurrentUser().getUsername());
        	form.setScreenName(userSessionComponent.getCurrentUser().getScreenName());
        	model.addAttribute("user", form);
        	return UbicacionesRecursos.CHANGE_PASSWORD_PAGE.getUbicacion();
        }
        return "redirect:" + UbicacionesRecursos.USER_PROFILE_PAGE.getUbicacion();
    }

    @RequestMapping(value = "/app/denied")
    public String denied() {
        return "/app/access/denied";
    }
    @RequestMapping(value = "/app/login/failure")
    public String loginFailure() {
        String message = "Login Failure!";
        return "redirect:/app/login?message="+message;
    }
    @RequestMapping(value = "/app/logout/success")
    public String logoutSuccess(Model model) {
    	model.addAttribute("logoutSuccess", true);
        return "/app/access/login";
    }
    
    @RequestMapping(value = "/app/logout/timeout")
    public String sessionTimeout(Model model) {
    	model.addAttribute("timeout", true);
        return "/app/access/login";
    }

    /** Login form with error. */
    @RequestMapping("/app/login/error")
    public String loginError(Model model) {
        model.addAttribute("loginError", true);
        return "/app/access/login";
    }

    @RequestMapping("/app/error/error")
    public String loginError() {

        return "/app/error/error";
    }
    
    @RequestMapping(value = "/acceso/captcha")
    public @ResponseBody String captcha(
    		@RequestParam("defaultReal") String defaultReal, 
    		@RequestParam("defaultRealHash") String defaultRealHash) {
    	if (rpHash(defaultReal).equals(defaultRealHash)) {
			// Accepted
    		return "1";
		}
		else {
			// Rejected
			return "0";
		}
    }
    
    private String rpHash(String value) {
		int hash = 5381;
		value = value.toUpperCase();
		for(int i = 0; i < value.length(); i++) {
			hash = ((hash << 5) + hash) + value.charAt(i);
		}
		return String.valueOf(hash);
	}
    
    @RequestMapping("/app/user/developers")
    public String developers() {

        return "/app/user/developers";
    }
}
